package com.ld.admin.websocket.chat.transformer;

import java.util.Date;

import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;

import com.alibaba.fastjson.JSONObject;
import com.ld.admin.websocket.chat.model.ChatMessage;

/**
 * 接收信息处理器，接收客户端发送的信息并转换为ChatMessage
 * 
 * @ClassName ChatMessageReceiver
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年2月27日 上午10:49:12
 *
 */
public class ChatMessageReceiver implements Decoder.Text<ChatMessage> {
    @Override
    public void init(final EndpointConfig config) {
    }

    @Override
    public void destroy() {
    }

    @Override
    public ChatMessage decode(final String textMessage) throws DecodeException {
        ChatMessage chatMessage = new ChatMessage();
        chatMessage = JSONObject.parseObject(textMessage, chatMessage.getClass());
//        JSONObject obj = (JSONObject) JSON.parse(textMessage);
//        chatMessage.setMessage(obj.getString("message"));
//        chatMessage.setSender(obj.getString("sender"));
//        chatMessage.setSenderName(obj.getString("senderName"));
//        chatMessage.setSendAll(obj.getInteger("sendAll"));
//        chatMessage.setReceiver(obj.getString("receiver"));
        chatMessage.setReceived(new Date());
        return chatMessage;
    }

    @Override
    public boolean willDecode(final String s) {
        return true;
    }
}
